import { Component } from '@angular/core';
import {
  AlertController, App, IonicPage, LoadingController, NavController, NavParams, Platform,
  ToastController,
  Item
} from 'ionic-angular';
import { ApiProvider } from "../../providers/api/api";
import { DatePipe } from "@angular/common";
/**
 * Generated class for the TeacheraddClassPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-teacheradd-class',
  templateUrl: 'teacheradd-class.html',
})
export class TeacheraddClassPage {

  constructor(private alertCtrl: AlertController, public navCtrl: NavController, public navParams: NavParams,
    public loadingCtrl: LoadingController,
    public http: ApiProvider, private dataPipe: DatePipe,
  ) {
  }
  classcourseId;
  classcourseNum;
  unitId;
  page = 1;
  pageNum;
  StudentList;
  teacherId;
  slides: Array<any> = [];
  total //数据数
  //getPage = this.page;//获取当前页码
  ionViewDidLoad() {
    //classcourseId:this.classCourseId,classcourseNum:this.classcourse_num,unitId:this.unitId
    this.classcourseId = this.navParams.get("classcourseId");
    this.classcourseNum=localStorage.getItem('classcourse_num');
    this.unitId = this.navParams.get("unitId");
    //this.teacherId = this.navParams.get("teacherId");
    this.teacherId = localStorage.getItem('teacherId');
  }
  ionViewDidEnter() {
    // console.log("ionViewDidEnter run");
    this.LoadList(1);
  }

  /**获取列表 */
  LoadList(e) {


    this.http.post("/teacherStudentController/getTeaStudentList.do", { teacherId: this.teacherId, page: e, rows: 5, classCourseId: this.classcourseId, applyState: 1 }).subscribe(item => {
      this.slides = item['rows']
      this.slides.forEach(e => {
        e.apply_date.time = this.dataPipe.transform(e.apply_date.time, 'yyyy-MM-dd');
        e.image_path = this.http.filePath + e.image_path
      })
      console.log(1233)
      this.total = item['total']
      console.log(item);
      //console.log(item)
      console.log(this.total % 5)
    })
  }

  /**
 * 下拉刷新
 * @param refresher
 */
  async doRefresh(refresher) {
    // alert(99)
    let loading = this.loadingCtrl.create({
      content: '刷新中...',
      showBackdrop: false
    });
    loading.present();
    this.LoadList(1);

    setTimeout(() => {
      refresher.complete();
      loading.dismiss();
    }, 2000);
  }

  doSearch(page) {
    this.page = page;
    this.LoadList(page);
  }

  //点击请出提示
  presentConfirm(Id,s,name) {
    let alert = this.alertCtrl.create({
      title: '提示！',
      message: `确认请出${name}吗？`,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          handler: () => {

          }
        },
        {
          text: '确认',
          handler: () => {
            this.http.post("/teacherStudentController/updateStudentTeacherState.do", { "teacherId": this.teacherId, "studentId": Id, "applyState": "3", "classCourseId": this.classcourseId }).subscribe(
              res => {
                //console.log(res)
              })
            if (this.total > 5 && this.total % 5 == 1) {
              this.LoadList(this.page - 1)
            } else {
              this.LoadList(this.page)

            }
          }
        }
      ]
    });
    alert.present();
  }

  goOut(Id: number,s,name) {
    this.presentConfirm(Id,s,name);

  }

}
